import tkinter as tk
from tkinter import messagebox
import sys
import json
import os
from Config import ConfigFile
class InputDialog:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("输入信息")

        # 设置窗口大小
        self.root.geometry("600x350")

        # 计算并设置窗口居中
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        x = (screen_width // 2) - (600 // 2)
        y = (screen_height // 2) - (400 // 2)
        self.root.geometry(f"600x400+{x}+{y}")

        # 创建输入框和标签
        tk.Label(self.root, text="芯对讲自动化地址：").grid(row=0, column=0, padx=10, pady=10)
        self.web_url_entry = tk.Entry(self.root, width=30)
        self.web_url_entry.grid(row=0, column=1, padx=10, pady=10)

        tk.Label(self.root, text="代理商登陆账号：").grid(row=1, column=0, padx=10, pady=10)
        self.agent_account_entry = tk.Entry(self.root, width=30)
        self.agent_account_entry.grid(row=1, column=1, padx=10, pady=10)

        tk.Label(self.root, text="代理商登陆密码：").grid(row=2, column=0, padx=10, pady=10)
        self.agent_password_entry = tk.Entry(self.root, width=30)
        self.agent_password_entry.grid(row=2, column=1, padx=10, pady=10)

        tk.Label(self.root, text="单位登陆账号：").grid(row=3, column=0, padx=10, pady=10)
        self.unit_account_entry = tk.Entry(self.root, width=30)
        self.unit_account_entry.grid(row=3, column=1, padx=10, pady=10)

        tk.Label(self.root, text="单位登陆密码：").grid(row=4, column=0, padx=10, pady=10)
        self.unit_password_entry = tk.Entry(self.root, width=30)
        self.unit_password_entry.grid(row=4, column=1, padx=10, pady=10)

        tk.Label(self.root, text="mark标记：\n(可为空，为空就是所有用例，带！为非)").grid(row=5, column=0, padx=10, pady=10)
        self.mark_entry = tk.Entry(self.root, width=30)  # 拉长输入框
        self.mark_entry.grid(row=5, column=1, padx=10, pady=10)

        # 单选按钮
        self.selection = tk.StringVar(value="XIN")  # 默认选中第一个选项
        tk.Label(self.root, text="选择测试平台\n（芯对讲、芯海外、待定）：").grid(row=6, column=0, padx=10, pady=10)

        # 将单选按钮靠在一起
        tk.Radiobutton(self.root, text="XIN", variable=self.selection, value="XIN").grid(row=6, column=1, padx=0, pady=5, sticky='w')
        tk.Radiobutton(self.root, text="XIN-POC", variable=self.selection, value="XIN-POC").grid(row=6, column=1, padx=90, pady=5, sticky='w')
        tk.Radiobutton(self.root, text="待定", variable=self.selection, value="待定").grid(row=6, column=1, padx=200, pady=5, sticky='w')

        # 提交按钮
        submit_button = tk.Button(self.root, text="提交", command=self.submit)
        submit_button.grid(row=7, columnspan=2, pady=20)

        # 初始化参数变量
        self.web_url = None
        self.agent_account = None
        self.agent_password = None
        self.unit_account = None
        self.unit_password = None
        self.mark = None
        self.select = None

        # 加载上次保存的内容
        self.load_previous_inputs()

        # 绑定关闭事件
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

    def submit(self):
        # 获取输入框中的内容
        self.web_url = self.web_url_entry.get()
        self.agent_account = self.agent_account_entry.get()
        self.agent_password = self.agent_password_entry.get()
        self.unit_account = self.unit_account_entry.get()
        self.unit_password = self.unit_password_entry.get()
        self.mark = self.mark_entry.get()
        self.select = self.selection.get()  # 获取选中的单选项

        # 验证输入
        if all([self.web_url, self.agent_account, self.agent_password, self.unit_account, self.unit_password]):
            # 存储当前输入的数据
            self.save_inputs()

            messagebox.showinfo("请注意", f'选中的选项是: {self.select}\n'
                                          '代理商登录：\n'
                                          '1.用户要有一个叫【lyz用户1】\n'
                                          '2.群组要有一个叫【lyz群组1】\n'
                                          '3.代理商有一个叫【1234】\n'
                                          '4.单位名有一个叫【lyz自用】\n'
                                          '5.单位账号有个叫【lyzdw1】\n'
                                          '单位账号：\n'
                                          '1.围栏要有一个叫【围栏1】\n'
                                          '不然可能会导致用例执行失败')
            self.root.destroy()  # 关闭窗口
        else:
            messagebox.showwarning("输入错误", "请填写所有信息！")

    def on_closing(self):
        print("窗口关闭，程序结束")
        self.root.destroy()  # 关闭窗口
        sys.exit()  # 结束程序

    def save_inputs(self):
        """保存用户输入的数据到文件"""
        data = {
            "web_url": self.web_url,
            "agent_account": self.agent_account,
            "agent_password": self.agent_password,
            "unit_account": self.unit_account,
            "unit_password": self.unit_password,
            "mark": self.mark,
            "selection": self.select
        }
        with open(ConfigFile.input_data_path, "w") as file:
            json.dump(data, file)

    def load_previous_inputs(self):
        """加载上次保存的数据"""
        if os.path.exists(ConfigFile.input_data_path):
            with open(ConfigFile.input_data_path, "r") as file:
                data = json.load(file)
                self.web_url_entry.insert(0, data.get("web_url", ""))
                self.agent_account_entry.insert(0, data.get("agent_account", ""))
                self.agent_password_entry.insert(0, data.get("agent_password", ""))
                self.unit_account_entry.insert(0, data.get("unit_account", ""))
                self.unit_password_entry.insert(0, data.get("unit_password", ""))
                self.mark_entry.insert(0, data.get("mark", ""))
                self.selection.set(data.get("selection", "XIN"))  # 默认选中 "XIN"

    def run(self):
        self.root.mainloop()

# 使用示例
if __name__ == "__main__":
    dialog = InputDialog()
    dialog.run()
    # 这里可以直接使用输入的参数
    print("获取的参数：")
    print("芯对讲自动化地址:", dialog.web_url)
    print("代理商登陆账号:", dialog.agent_account)
    print("代理商登陆密码:", dialog.agent_password)
    print("单位登陆账号:", dialog.unit_account)
    print("单位登陆密码:", dialog.unit_password)
    print("mark标记：", dialog.mark)
    print(dialog.select)
